package cn.cloudreal.cbms.castDetail.action;

import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import cn.cloudreal.cbms.castDetail.service.CastDetailService;
import cn.cloudreal.cbms.castDetail.service.ChargeService;
import cn.cloudreal.cbms.common.util.Arith;
import cn.cloudreal.cbms.common.util.Constants;
import cn.cloudreal.cbms.common.util.DatadicContext;
import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.common.util.StringUtils;
import cn.cloudreal.cbms.log.service.OptLogService;
import cn.cloudreal.cbms.vo.Arrangement;
import cn.cloudreal.cbms.vo.ArrangementDate;
import cn.cloudreal.cbms.vo.CastDetail;
import cn.cloudreal.cbms.vo.Charge;
import cn.cloudreal.cbms.vo.Dictionary;
import cn.cloudreal.cbms.vo.Log;
import cn.cloudreal.cbms.vo.Material;
import cn.cloudreal.cbms.vo.MediaPrice;
import cn.cloudreal.cbms.vo.Mediasumm;
import cn.cloudreal.cbms.vo.Medium;
import cn.cloudreal.cbms.vo.Project;
import cn.cloudreal.cbms.vo.ProjectMedium;
import cn.cloudreal.cbms.vo.UserInfo;

import com.opensymphony.xwork2.ActionSupport;

/**
 * <p>
 * Title: CBM
 * </p>
 * <p>
 * Description:投放明细服务层视图类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * <p>
 * Company:云恒瑞通
 * </p>
 * 
 * @author WangTao
 * @date 2012-07-09
 */
public class CastDetailAction extends ActionSupport
{

    /** serialVersionUID */
    private static final long serialVersionUID = 1L;

    /** 日志工具类实例 */
    private static final Logger log = Logger.getLogger(CastDetailAction.class);

    /** 服务 */
    CastDetailService castDetailService;

    /** 服务 */
    ChargeService chargeService;

    /** 服务 */
    OptLogService optLogService;

    /** 当前页 */
    int currentPage;

    /** 分页对象 */
    PageBean page;

    /** 投放明细列表 */
    List<CastDetail> castDetails = new ArrayList<CastDetail>();

    /** 投放明细 */
    CastDetail castDetail;

    /** 项目开始时间 */
    String projectStartTime;

    /** 项目结束时间 */
    String projectEndTime;

    /** 媒介类型 */
    String mediumResourceType;

    /** 资源使用状态 */
    String mediumStatus;

    /** 地市 */
    String city;

    /** 关键字类型 */
    String keywordType;

    /** 关键字 */
    String keyword;

    /** 项目id */
    String sProjectId;

    /** 项目类型 */
    String projectType;

    /** 项目类型 */
    String g_materialLevel;

    /** 自增编号 */
    int proMedID;

    /** 媒体资源类型 */
    List<Dictionary> mediaResourceTypeSelect = new ArrayList<Dictionary>();

    /** 资源占用状态下拉 */
    List<Dictionary> mediumStatusSelect = new ArrayList<Dictionary>();

    /** 地市下拉 */
    List<Dictionary> citySelect = new ArrayList<Dictionary>();

    /** 关键字类型下拉 */
    List<Dictionary> keywordTypeSelect = new ArrayList<Dictionary>();

    /** 项目类型下拉 */
    List<Dictionary> projectTypeSelect = new ArrayList<Dictionary>();

    /**  */
    List<Dictionary> mediumPromotionTypeSelect = new ArrayList<Dictionary>();

    /** ajax返回 */
    List<Project> ajaxProjects;

    /** ajax返回 */
    List<Mediasumm> ajaxMedias;

    /** ajax返回 */
    List<Medium> ajaxMediums;

    /** ajax返回 */
    List<Material> ajaxMaterials;

    /** 基础数据插入 */
    ProjectMedium projectMedium;

    /** 排期初始值 */
    List<Arrangement> initArrangements;

    /** 费用 */
    List<Charge> charges;

    /** 列表显示详情日志 */
    List<Log> logList;

    String userRoleRange = "";

    /** 投放明细显示一级物料用 */
    Material g_material;

    boolean ajaxFlag;

    int castDetailstatus;

    int isInsertFlag;

    /**
     * 媒介投放明细列表
     * 
     * @return String
     */
    public String mediumCastDetailList()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction mediumCastDetailList() method, return String");
        }

        try
        {
            // 获取当前session对象中所属城市
            UserInfo userInfo = (UserInfo) ServletActionContext.getRequest()
                    .getSession()
                    .getAttribute(Constants.SESSIONPRKEY);
            // userInfo.getUserRole().setRoleRange("1");
            // userInfo.setUsercity("SGS");
            // 初始化分页对象
            page = new PageBean(currentPage == 0 ? 1 : currentPage,
                    Constants.PAGE_SIZE);
            // 非空
            if (null != userInfo)
            {
                //
                if (userInfo.getUserRole() != null)
                {
                    // 默认东莞
                    String userCity = "DG";
                    // 非空重置
                    if (userInfo.getUsercity() != null)
                    {
                        if (!"".equals(userInfo.getUsercity()))
                        {
                            userCity = userInfo.getUsercity();
                        }
                    }
                    // 操作范围
                    String roleRange = userInfo.getUserRole().getRoleRange();
                    //
                    if (roleRange != null)
                    {

                        // 省
                        if ("1".endsWith(roleRange))
                        {
                            //
                            userRoleRange = "1";
                            //
                            if (city == null || "".equals(city))
                            {
                                city = userCity;
                            }
                            // 条件
                            Map condition = new HashMap();

                            condition.put("mediumResourceType",
                                    mediumResourceType);
                            condition.put("mediumStatus", mediumStatus);
                            condition.put("keywordType", keywordType);
                            condition.put("keyword", keyword);
                            condition.put("projectType", projectType);
                            condition.put("contractYear",
                                    (new Date().getYear() + 1900) + "");
                            condition.put("city", city);
                            condition.put("creator", null);
                            if (projectStartTime != null)
                            {
                                if (!"".equals(projectStartTime))
                                {
                                    condition.put("projectStartTime",
                                            projectStartTime + "-01");
                                }
                                else
                                {
                                    condition.put("projectStartTime",
                                            projectStartTime);
                                }
                            }
                            if (projectEndTime != null)
                            {
                                if (!"".equals(projectEndTime))
                                {
                                    condition.put("projectEndTime",
                                            projectEndTime + "-01");
                                }
                                else
                                {
                                    condition.put("projectEndTime",
                                            projectEndTime);
                                }
                            }
                            //
                            mediaResourceTypeSelect = DatadicContext.getInstance()
                                    .getDicDataListByCode("MTZYLX");
                            //
                            mediumStatusSelect = DatadicContext.getInstance()
                                    .getDicDataListByCode("MJSYQK");
                            //
                            citySelect = DatadicContext.getInstance()
                                    .getDicDataListByCode("CITY");
                            //
                            // keywordTypeSelect =
                            // DatadicContext.getInstance().getDicDataListByCode("CITY");
                            //
                            projectTypeSelect = DatadicContext.getInstance()
                                    .getDicDataListByCode("XMLX");

                            mediumPromotionTypeSelect = DatadicContext.getInstance()
                                    .getDicDataListByCode("MJZSLX");

                            // 获取总记录数
                            int totalRecords = castDetailService.getCastDetailCount(condition);
                            // 设置分页对象总记录数
                            page.setTotalRecords(totalRecords);
                            // 查询
                            castDetails = castDetailService.getMediumCastDetailList(condition,
                                    page);
                            if (castDetails != null)
                            {
                                // 遍历
                                for (CastDetail castDetail : castDetails)
                                {
                                    // 物料编号非空
                                    if (castDetail.getId_material() != null)
                                    {
                                        if (!"".equals(castDetail.getId_material()))
                                        {
                                            Material material1 = new Material();
                                            material1.setMaterialID(castDetail.getId_material());
                                            ajaxMaterials = castDetailService.ajaxGetMaterial(material1);
                                            //
                                            castDetail.setId_material(castDetail.getId_material()
                                                    .substring(1,
                                                            castDetail.getId_material()
                                                                    .length() - 1));
                                            castDetail.setId_materialArray(castDetail.getId_material()
                                                    .split(","));
                                            System.out.println(castDetail.getId_materialArray()
                                                    + ":"
                                                    + castDetail.getId_material());
                                            String[][] array = new String[ajaxMaterials.size()][2];
                                            //
                                            for (int i = 0; i < ajaxMaterials.size(); i++)
                                            {
                                                Material mat = ajaxMaterials.get(i);
                                                array[i][0] = mat.getMatId()
                                                        + "";
                                                array[i][1] = mat.getMaterialID();
                                            }
                                            castDetail.setMaterialsArray(array);
                                            System.out.println();
                                        }
                                        // for (int i = 0; i <
                                        // castDetail.getId_materialArray().length;
                                        // i++)
                                        /*
                                         * Material material1 = new Material();
                                         * material1
                                         * .setMaterialID(castDetail.getId_material
                                         * ()); ajaxMaterials =
                                         * castDetailService
                                         * .ajaxGetMaterial(material1);
                                         */
                                        // {
                                        // System.out.print(castDetail.getId_materialArray()[i]);

                                        // }
                                    }
                                }
                            }
                        }
                        // 地市级
                        else if ("2".endsWith(roleRange))
                        {
                            //
                            userRoleRange = "2";
                            //
                            city = userCity;
                            //
                            if (city == null || "".equals(city))
                            {
                                city = userCity;
                            }
                            // 条件
                            Map condition = new HashMap();
                            if (projectStartTime != null)
                            {
                                if (!"".equals(projectStartTime))
                                {
                                    condition.put("projectStartTime",
                                            projectStartTime + "-01");
                                }
                                else
                                {
                                    condition.put("projectStartTime",
                                            projectStartTime);
                                }
                            }
                            if (projectEndTime != null)
                            {
                                if (!"".equals(projectEndTime))
                                {
                                    condition.put("projectEndTime",
                                            projectEndTime + "-01");
                                }
                                else
                                {
                                    condition.put("projectEndTime",
                                            projectEndTime);
                                }
                            }
                            condition.put("mediumResourceType",
                                    mediumResourceType);
                            condition.put("mediumStatus", mediumStatus);
                            condition.put("keywordType", keywordType);
                            condition.put("keyword", keyword);
                            condition.put("projectType", projectType);
                            condition.put("contractYear",
                                    (new Date().getYear() + 1900) + "");
                            condition.put("city", city);
                            condition.put("creator", null);
                            //
                            mediaResourceTypeSelect = DatadicContext.getInstance()
                                    .getDicDataListByCode("MTZYLX");
                            //
                            mediumStatusSelect = DatadicContext.getInstance()
                                    .getDicDataListByCode("MJSYQK");
                            //
                            Dictionary cityDictionary = DatadicContext.getInstance()
                                    .getDatadicObject(city, "CITY");
                            if (cityDictionary != null)
                            {
                                citySelect.add(cityDictionary);
                            }
                            //
                            // keywordTypeSelect =
                            // DatadicContext.getInstance().getDicDataListByCode("CITY");
                            //
                            projectTypeSelect = DatadicContext.getInstance()
                                    .getDicDataListByCode("XMLX");

                            mediumPromotionTypeSelect = DatadicContext.getInstance()
                                    .getDicDataListByCode("MJZSLX");

                            // 获取总记录数
                            int totalRecords = castDetailService.getCastDetailCount(condition);
                            // 设置分页对象总记录数
                            page.setTotalRecords(totalRecords);
                            // 查询
                            castDetails = castDetailService.getMediumCastDetailList(condition,
                                    page);
                            if (castDetails != null)
                            {
                                // 遍历
                                for (CastDetail castDetail : castDetails)
                                {
                                    // 物料编号非空
                                    if (castDetail.getId_material() != null)
                                    {
                                        if (!"".equals(castDetail.getId_material()))
                                            // 分割物料编号
                                            castDetail.setId_materialArray(castDetail.getId_material()
                                                    .split(","));
                                    }
                                }
                            }
                        }
                        // 个人
                        else if ("3".endsWith(roleRange))
                        {
                            //
                            userRoleRange = "3";
                            //
                            city = userCity;
                            //
                            if (city == null || "".equals(city))
                            {
                                city = userCity;
                            }
                            // 条件
                            Map condition = new HashMap();
                            if (projectStartTime != null)
                            {
                                if (!"".equals(projectStartTime))
                                {
                                    condition.put("projectStartTime",
                                            projectStartTime + "-01");
                                }
                                else
                                {
                                    condition.put("projectStartTime",
                                            projectStartTime);
                                }
                            }
                            if (projectEndTime != null)
                            {
                                if (!"".equals(projectEndTime))
                                {
                                    condition.put("projectEndTime",
                                            projectEndTime + "-01");
                                }
                                else
                                {
                                    condition.put("projectEndTime",
                                            projectEndTime);
                                }
                            }
                            condition.put("mediumResourceType",
                                    mediumResourceType);
                            condition.put("mediumStatus", mediumStatus);
                            condition.put("keywordType", keywordType);
                            condition.put("keyword", keyword);
                            condition.put("projectType", projectType);
                            condition.put("contractYear",
                                    (new Date().getYear() + 1900) + "");
                            condition.put("city", city);
                            condition.put("creator", userInfo.getUserCode());
                            //
                            mediaResourceTypeSelect = DatadicContext.getInstance()
                                    .getDicDataListByCode("MTZYLX");
                            //
                            mediumStatusSelect = DatadicContext.getInstance()
                                    .getDicDataListByCode("MJSYQK");
                            //
                            Dictionary cityDictionary = DatadicContext.getInstance()
                                    .getDatadicObject(city, "CITY");
                            if (cityDictionary != null)
                            {
                                citySelect.add(cityDictionary);
                            }
                            //
                            // keywordTypeSelect =
                            // DatadicContext.getInstance().getDicDataListByCode("CITY");
                            //
                            projectTypeSelect = DatadicContext.getInstance()
                                    .getDicDataListByCode("XMLX");

                            mediumPromotionTypeSelect = DatadicContext.getInstance()
                                    .getDicDataListByCode("MJZSLX");

                            // 获取总记录数
                            int totalRecords = castDetailService.getCastDetailCount(condition);
                            // 设置分页对象总记录数
                            page.setTotalRecords(totalRecords);
                            // 查询
                            castDetails = castDetailService.getMediumCastDetailList(condition,
                                    page);
                            if (castDetails != null)
                            {
                                // 遍历
                                for (CastDetail castDetail : castDetails)
                                {
                                    // 物料编号非空
                                    if (castDetail.getId_material() != null)
                                    {
                                        if (!"".equals(castDetail.getId_material()))
                                            // 分割物料编号
                                            castDetail.setId_materialArray(castDetail.getId_material()
                                                    .split(","));
                                    }
                                }
                            }
                        }

                    }
                }
            }

            // 重置返回
            resultName = SUCCESS;
        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction mediumCastDetailList() method",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction mediumCastDetailList() method, return String");
        }
        return resultName;
    }

    /**
     * 删除投放明细
     * 
     * @return String
     */
    public String deleteCastDetail() throws Exception
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction deleteCastDetail() method, return String");
        }
        try
        {
            ProjectMedium pm = castDetailService.getProjectMediumByProMedID(projectMedium.getProMedID());
            if (pm != null)
            {
                sProjectId = pm.getProjectID();
                //
                if (Constants.MJSYQK_4.equals(pm.getMediumStatus())
                        || Constants.MJSYQK_5.equals(pm.getMediumStatus()))
                {
                    HttpServletResponse response = ServletActionContext.getResponse();
                    response.setContentType("text/html; charset=utf-8");
                    PrintWriter out = response.getWriter();
                    out.println("<script type=\"text/javascript\">");
                    out.println("alert(\"该条记录已完成投放或已完成结算不可删除！\");");
                    if (castDetailstatus == 1)
                    {
                        // 返回项目详细
                        out.println("window.location.href='proDetail.action?projectId="
                                + sProjectId + "';");
                    }
                    else if (castDetailstatus == 2)
                    {
                        // 返回项目列表
                        out.println("window.location.href='proQuery.action?opflag=init';");
                    }
                    else
                    {
                        // 返回投放明细列表
                        out.println("window.location.href='mediumCastDetailList.action';");
                    }
                    out.println("</script>");
                    out.flush();
                    out.close();
                }
                else
                {
                    Medium m = castDetailService.getMediumByMediumID(pm.getId_medium());
                    String[] mediumArrangement = pm.getMediumArrangement()
                            .split(",");
                    String[] totalMediumArrangement = m.getMediumArrangement()
                            .split(",");
                    for (int i = 0; i < totalMediumArrangement.length; i++)
                    {
                        totalMediumArrangement[i] = Integer.valueOf(totalMediumArrangement[i])
                                - Integer.valueOf(mediumArrangement[i]) + "";
                    }
                    // 转字符串
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < totalMediumArrangement.length; i++)
                    {
                        sb.append("," + totalMediumArrangement[i]);
                    }
                    m.setMediumArrangement(sb.toString().substring(1));
                    // 删除费用
                    chargeService.delProjectMedioPrice(pm.getProMedID());
                    // 删除
                    if (castDetailService.deleteCastDetail(pm, m))
                    {
                        //
                        UserInfo userInfo = (UserInfo) ServletActionContext.getRequest()
                                .getSession()
                                .getAttribute(Constants.SESSIONPRKEY);
                        // 日志记录
                        String user = null;
                        String cityCode = null;
                        String cityName = null;
                        String organizaOragName = null;
                        if (null == userInfo)
                        {
                            user = "张三";
                            cityName = "东莞";
                            organizaOragName = "市场部";
                            cityCode = "DG";
                        }
                        else
                        {
                            user = userInfo.getUserName();
                            cityCode = userInfo.getUsercity();
                            cityName = userInfo.getCityName();
                            organizaOragName = userInfo.getOrganiza()
                                    .getOrgName();
                        }
                        if (null == cityCode)
                        {
                            cityCode = "";
                            cityName = "东莞";
                        }
                        Timestamp d = new Timestamp(System.currentTimeMillis());
                        String content = d + " " + cityName + organizaOragName
                                + user + "删除了投放明细";
                        String datatime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
                        Date data = new Date(System.currentTimeMillis());
                        /** 获取变更记录的数据 */
                        Log logRecord = new Log("T_CB_PROJECTMEDIUM",
                                content,
                                pm.getProMedID() + "",
                                datatime,
                                user,
                                cityCode,
                                organizaOragName,
                                "科室");

                        optLogService.addLog(logRecord);
                        // 重置返回值
                        if (castDetailstatus == 1)
                        {
                            HttpServletResponse response = ServletActionContext.getResponse();
                            response.setContentType("text/html; charset=utf-8");
                            PrintWriter out = response.getWriter();
                            out.println("<script type=\"text/javascript\">");
                            out.println("alert(\"删除成功！\");");
                            out.println("window.location.href='proDetail.action?projectId="
                                    + sProjectId + "';");
                            out.println("</script>");
                            out.flush();
                            out.close();
                            // 返回项目详细
                            // resultName = SUCCESS + "1";
                        }
                        else if (castDetailstatus == 2)
                        {
                            HttpServletResponse response = ServletActionContext.getResponse();
                            response.setContentType("text/html; charset=utf-8");
                            PrintWriter out = response.getWriter();
                            out.println("<script type=\"text/javascript\">");
                            out.println("alert(\"删除成功！\");");
                            out.println("window.location.href='proQuery.action?opflag=init';");
                            out.println("</script>");
                            out.flush();
                            out.close();
                            // 返回项目列表
                            // resultName = SUCCESS + "2";
                        }
                        else
                        {
                            // 返回投放明细列表
                            resultName = SUCCESS;
                        }
                    }
                    else
                    {
                        HttpServletResponse response = ServletActionContext.getResponse();
                        response.setContentType("text/html; charset=utf-8");
                        PrintWriter out = response.getWriter();
                        out.println("<script type=\"text/javascript\">");
                        out.println("alert(\"删除不成功！\");");
                        if (castDetailstatus == 1)
                        {
                            // 返回项目详细
                            out.println("window.location.href='proDetail.action?projectId="
                                    + sProjectId + "';");
                        }
                        else if (castDetailstatus == 2)
                        {
                            // 返回项目列表
                            out.println("window.location.href='proQuery.action?opflag=init';");
                        }
                        else
                        {
                            // 返回投放明细列表
                            out.println("window.location.href='mediumCastDetailList.action';");
                        }
                        out.println("</script>");
                        out.flush();
                        out.close();
                    }
                }

            }
            else
            {
                // 重置返回值
                resultName = "recordException";
            }

        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction deleteCastDetail() method",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }
        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction deleteCastDetail() method, return String");
        }
        return resultName;
    }

    /**
     * 基本信息录入初始化
     * 
     * @return String
     */
    public String initInsertBasicInfo()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction initInsertBasicInfo() method, return String");
        }

        try
        {
            // 下拉菜单
            mediumStatusSelect = DatadicContext.getInstance()
                    .getDicDataListByCode("MJSYQK");
            // 查询媒体
            ajaxGetMedia();
            // 重置返回值
            resultName = SUCCESS;
            // 项目库来源
            if (castDetailstatus == 1)
            {
                if (projectMedium != null)
                {
                    if (projectMedium.getProjectID() != null)
                    {
                        Project p1 = new Project();
                        p1.setProjectId(projectMedium.getProjectID());
                        ajaxProjects = castDetailService.ajaxGetProject(p1);
                        if (ajaxProjects != null)
                        {
                            if (ajaxProjects.size() != 0)
                            {
                                projectMedium.setProjectName(ajaxProjects.get(0)
                                        .getProjectName());
                                projectMedium.setMediumStatus(ajaxProjects.get(0)
                                        .getProCurStatus());
                                // 重置返回值
                                resultName = SUCCESS + "2";
                            }
                        }
                    }

                }
            }
            // 直接添加
        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction initInsertBasicInfo() method, return String",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction initInsertBasicInfo() method, return String");
        }
        return resultName;
    }

    /**
     * 基本信息录入
     * 
     * @return String
     */
    public String insertBasicInfo()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction insertBasicInfo() method, return String");
        }

        try
        {
            UserInfo userInfo = (UserInfo) ServletActionContext.getRequest()
                    .getSession()
                    .getAttribute(Constants.SESSIONPRKEY);
            //
            projectMedium.setCreator(userInfo.getUserCode());
            //
            if (castDetailService.insertBasicInfo(projectMedium) != -9527)
            {
                // 日志记录
                String user = null;
                String cityCode = null;
                String cityName = null;
                String organizaOragName = null;
                if (null == userInfo)
                {
                    user = "张三";
                    cityName = "东莞";
                    organizaOragName = "市场部";
                    cityCode = "DG";
                }
                else
                {
                    user = userInfo.getUserName();
                    cityCode = userInfo.getUsercity();
                    cityName = userInfo.getCityName();
                    organizaOragName = userInfo.getOrganiza().getOrgName();
                }
                if (null == cityCode)
                {
                    cityCode = "";
                    cityName = "东莞";
                }
                Timestamp d = new Timestamp(System.currentTimeMillis());
                String content = d + " " + cityName + organizaOragName + user
                        + "创建了基本信息";
                String datatime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
                Date data = new Date(System.currentTimeMillis());
                /** 获取变更记录的数据 */
                Log logRecord = new Log("T_CB_PROJECTMEDIUM",
                        content,
                        projectMedium.getProMedID() + "",
                        datatime,
                        user,
                        cityCode,
                        organizaOragName,
                        "科室");

                optLogService.addLog(logRecord);
                // 重置返回值
                resultName = SUCCESS;
            }
            else
            {
                HttpServletResponse response = ServletActionContext.getResponse();
                response.setContentType("text/html; charset=utf-8");
                PrintWriter out = response.getWriter();
                out.println("<script type=\"text/javascript\">");
                out.println("alert(\"已有相同数据存在，不能重复新增！\");");
                out.println("window.location.href='initCastDetailHome.action';");
                out.println("</script>");
                out.flush();
                out.close();
            }

        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction insertBasicInfo() method",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction insertBasicInfo() method, return String");
        }
        return resultName;
    }

    /**
     * 基本信息编辑初始化
     * 
     * @return String
     */
    public String initEditBasicInfo()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction initInsertBasicInfo() method, return String");
        }

        try
        {
            // 下拉菜单
            mediumStatusSelect = DatadicContext.getInstance()
                    .getDicDataListByCode("MJSYQK");
            // 查询媒体
            castDetail = castDetailService.getMediumCastDetail(projectMedium);
            //
            if (castDetail != null)
            {
                if (isInsertFlag == 1)
                {
                    // 重置返回值
                    resultName = SUCCESS;
                }
                else
                {
                    if (Constants.MJSYQK_4.equals(castDetail.getMediumStatus())
                            || Constants.MJSYQK_5.equals(castDetail.getMediumStatus())
                            || Constants.MJSYQK_6.equals(castDetail.getMediumStatus()))
                    {
                        HttpServletResponse response = ServletActionContext.getResponse();
                        response.setContentType("text/html; charset=utf-8");
                        PrintWriter out = response.getWriter();
                        out.println("<script type=\"text/javascript\">");
                        out.println("alert(\"该条记录已完成投放或已完成结算或已中止不可编辑！\");");
                        out.println("window.location.href='mediumCastDetailList.action';");
                        out.println("</script>");
                        out.flush();
                        out.close();
                    }
                    else
                    {
                        // 重置返回值
                        resultName = SUCCESS;
                    }
                }
            }
            else
            {
                // 重置返回值
                resultName = "recordException";
            }

        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction initInsertBasicInfo() method, return String",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction initInsertBasicInfo() method, return String");
        }
        return resultName;
    }

    /**
     * 基本信息编辑
     * 
     * @return String
     */
    public String editBasicInfo()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction editBasicInfo() method, return String");
        }

        try
        {
            projectMedium = new ProjectMedium();
            // set
            projectMedium.setProMedID(castDetail.getProMedID());
            // 查询媒体
            CastDetail cd = castDetailService.getMediumCastDetail(projectMedium);
            //
            if (cd != null)
            {
                // if (Constants.MJSYQK_4.equals(cd.getMediumStatus())
                // || Constants.MJSYQK_5.equals(cd.getMediumStatus())
                // || Constants.MJSYQK_6.equals(cd.getMediumStatus()))
                // {
                // HttpServletResponse response =
                // ServletActionContext.getResponse();
                // response.setContentType("text/html; charset=utf-8");
                // PrintWriter out = response.getWriter();
                // out.println("<script type=\"text/javascript\">");
                // out.println("alert(\"该条记录已完成投放或已完成结算或已中止不可编辑！\");");
                // out.println("window.location.href='mediumCastDetailList.action';");
                // out.println("</script>");
                // out.flush();
                // out.close();
                // }
                // else
                // {
                castDetail.setArrangeTime(castDetail.getArrangeTime() + "-01");
                // 更新
                if (castDetailService.updateMediumCastDetail(castDetail, 1) != 9527)
                {
                    // 重置返回值
                    resultName = SUCCESS;
                    // 日志
                    UserInfo userInfo = (UserInfo) ServletActionContext.getRequest()
                            .getSession()
                            .getAttribute(Constants.SESSIONPRKEY);
                    // 日志记录
                    String user = null;
                    String cityCode = null;
                    String cityName = null;
                    String organizaOragName = null;
                    if (null == userInfo)
                    {
                        user = "张三";
                        cityName = "东莞";
                        organizaOragName = "市场部";
                        cityCode = "DG";
                    }
                    else
                    {
                        user = userInfo.getUserName();
                        cityCode = userInfo.getUsercity();
                        cityName = userInfo.getCityName();
                        organizaOragName = userInfo.getOrganiza().getOrgName();
                    }
                    if (null == cityCode)
                    {
                        cityCode = "";
                        cityName = "东莞";
                    }
                    Timestamp d = new Timestamp(System.currentTimeMillis());
                    String content = d + " " + cityName + organizaOragName
                            + user + "编辑了基本信息";
                    String datatime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
                    Date data = new Date(System.currentTimeMillis());
                    /** 获取变更记录的数据 */
                    Log logRecord = new Log("T_CB_PROJECTMEDIUM",
                            content,
                            castDetail.getProMedID() + "",
                            datatime,
                            user,
                            cityCode,
                            organizaOragName,
                            "科室");

                    optLogService.addLog(logRecord);
                }
                else
                {
                    HttpServletResponse response = ServletActionContext.getResponse();
                    response.setContentType("text/html; charset=utf-8");
                    PrintWriter out = response.getWriter();
                    out.println("<script type=\"text/javascript\">");
                    out.println("alert(\"已有相同数据存在，编辑失败！\");");
                    out.println("window.location.href='initCastDetailHome.action?projectMedium.proMedID="
                            + projectMedium.getProMedID() + "';");
                    out.println("</script>");
                    out.flush();
                    out.close();
                }

                // }
            }
            else
            {
                // 重置返回值
                resultName = "recordException";
            }

        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction editBasicInfo() method, return String",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction editBasicInfo() method, return String");
        }
        return resultName;
    }

    /**
     * 物料信息录入初始化
     * 
     * @return String
     */
    public String initInsertMaterialInfo()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction initInsertMaterialInfo() method, return String");
        }

        try
        {
            // 查询
            castDetail = castDetailService.getMediumCastDetail(projectMedium);
            // 重置返回值
            resultName = SUCCESS;
        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction initInsertMaterialInfo() method, return String",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction initInsertMaterialInfo() method, return String");
        }
        return resultName;
    }

    /**
     * 物料信息录入
     * 
     * @return String
     */
    public String insertMaterialInfo()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction insertMaterialInfo() method, return String");
        }

        try
        {
            // 插入
            castDetailService.insertMaterialInfo(projectMedium);
            // 重置返回值
            resultName = SUCCESS;
            // 日志
            UserInfo userInfo = (UserInfo) ServletActionContext.getRequest()
                    .getSession()
                    .getAttribute(Constants.SESSIONPRKEY);
            // 日志记录
            String user = null;
            String cityCode = null;
            String cityName = null;
            String organizaOragName = null;
            if (null == userInfo)
            {
                user = "张三";
                cityName = "东莞";
                organizaOragName = "市场部";
                cityCode = "DG";
            }
            else
            {
                user = userInfo.getUserName();
                cityCode = userInfo.getUsercity();
                cityName = userInfo.getCityName();
                organizaOragName = userInfo.getOrganiza().getOrgName();
            }
            if (null == cityCode)
            {
                cityCode = "";
                cityName = "东莞";
            }
            Timestamp d = new Timestamp(System.currentTimeMillis());
            String content = d + " " + cityName + organizaOragName + user
                    + "创建了物料信息";
            String datatime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
            Date data = new Date(System.currentTimeMillis());
            /** 获取变更记录的数据 */
            Log logRecord = new Log("T_CB_PROJECTMEDIUM",
                    content,
                    projectMedium.getProMedID() + "",
                    datatime,
                    user,
                    cityCode,
                    organizaOragName,
                    "科室");

            optLogService.addLog(logRecord);
        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction insertMaterialInfo() method, return String",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction insertMaterialInfo() method, return String");
        }
        return resultName;
    }

    /**
     * 物料信息编辑初始化
     * 
     * @return String
     */
    public String initEditMaterialInfo()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction initInsertMaterialInfo() method, return String");
        }

        try
        {
            // 实例化对象
            projectMedium = new ProjectMedium();
            projectMedium.setProMedID(castDetail.getProMedID());
            // 查询
            castDetail = castDetailService.getMediumCastDetail(projectMedium);
            //
            if (castDetail != null)
            {
                if (isInsertFlag == 1)
                {
                    if (castDetail.getId_material() != null
                            && !"".equals(castDetail.getId_material()))
                    {
                        Material material = new Material();
                        material.setMaterialID(castDetail.getId_material());
                        ajaxMaterials = castDetailService.ajaxGetMaterial(material);
                        if (ajaxMaterials != null)
                        {
                            if (ajaxMaterials.size() != 0)
                            {
                                StringBuffer materialNames = new StringBuffer();
                                for (int i = 0; i < ajaxMaterials.size(); i++)
                                {
                                    if (ajaxMaterials.get(i)
                                            .getMaterialLevel()
                                            .equals("1"))
                                    {
                                        g_material = ajaxMaterials.get(i);
                                        continue;
                                    }

                                    materialNames.append(ajaxMaterials.get(i)
                                            .getMaterialID());
                                    if (i != (ajaxMaterials.size() - 1))
                                    {
                                        materialNames.append("\n");
                                    }
                                }
                                castDetail.setMaterialNames(materialNames.toString());
                            }
                        }

                        if (castDetail.getId_material().length() > 2)
                        {
                            castDetail.setId_material(castDetail.getId_material()
                                    .substring(1,
                                            (castDetail.getId_material()
                                                    .length() - 1)));
                        }
                        else
                        {
                            castDetail.setId_material("");
                        }
                    }
                    // 重置返回值
                    resultName = SUCCESS;
                }
                else
                {

                    if (Constants.MJSYQK_4.equals(castDetail.getMediumStatus())
                            || Constants.MJSYQK_5.equals(castDetail.getMediumStatus())
                            || Constants.MJSYQK_6.equals(castDetail.getMediumStatus()))
                    {
                        HttpServletResponse response = ServletActionContext.getResponse();
                        response.setContentType("text/html; charset=utf-8");
                        PrintWriter out = response.getWriter();
                        out.println("<script type=\"text/javascript\">");
                        // out.println("alert(\"该条记录已完成投放或已完成结算或已中止不可编辑！\");");
                        out.println("window.location.href='mediumCastDetailList.action';");
                        out.println("</script>");
                        out.flush();
                        out.close();
                    }
                    else
                    {
                        if (castDetail.getId_material() != null
                                && !"".equals(castDetail.getId_material()))
                        {
                            Material material = new Material();
                            material.setMaterialID(castDetail.getId_material());
                            ajaxMaterials = castDetailService.ajaxGetMaterial(material);
                            if (ajaxMaterials != null)
                            {
                                if (ajaxMaterials.size() != 0)
                                {
                                    StringBuffer materialNames = new StringBuffer();
                                    for (int i = 0; i < ajaxMaterials.size(); i++)
                                    {
                                        if (ajaxMaterials.get(i)
                                                .getMaterialLevel()
                                                .equals("1"))
                                        {
                                            g_material = ajaxMaterials.get(i);
                                            continue;
                                        }

                                        materialNames.append(ajaxMaterials.get(i)
                                                .getMaterialID());
                                        if (i != (ajaxMaterials.size() - 1))
                                        {
                                            materialNames.append("\n");
                                        }
                                    }
                                    castDetail.setMaterialNames(materialNames.toString());
                                }
                            }

                            if (castDetail.getId_material().length() > 2)
                            {
                                castDetail.setId_material(castDetail.getId_material()
                                        .substring(1,
                                                (castDetail.getId_material()
                                                        .length() - 1)));
                            }
                            else
                            {
                                castDetail.setId_material("");
                            }
                        }
                        // 重置返回值
                        resultName = SUCCESS;
                    }
                }
            }
            else
            {
                // 重置返回值
                resultName = "recordException";
            }

        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction initInsertMaterialInfo() method, return String",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction initInsertMaterialInfo() method, return String");
        }
        return resultName;
    }

    /**
     * 物料信息编辑
     * 
     * @return String
     */
    public String editMaterialInfo()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction editMaterialInfo() method, return String");
        }

        try
        {
            projectMedium = new ProjectMedium();
            // set
            projectMedium.setProMedID(castDetail.getProMedID());
            // 查询媒体
            CastDetail cd = castDetailService.getMediumCastDetail(projectMedium);
            //
            if (cd != null)
            {
                // if (Constants.MJSYQK_4.equals(cd.getMediumStatus())
                // || Constants.MJSYQK_5.equals(cd.getMediumStatus())
                // || Constants.MJSYQK_6.equals(cd.getMediumStatus()))
                // {
                // HttpServletResponse response =
                // ServletActionContext.getResponse();
                // response.setContentType("text/html; charset=utf-8");
                // PrintWriter out = response.getWriter();
                // out.println("<script type=\"text/javascript\">");
                // out.println("alert(\"该条记录已完成投放或已完成结算或已中止不可编辑！\");");
                // out.println("window.location.href='mediumCastDetailList.action';");
                // out.println("</script>");
                // out.flush();
                // out.close();
                // }
                // else
                // {
                // 更新
                castDetailService.updateMediumCastDetail(castDetail, 2);
                // 重置返回值
                resultName = SUCCESS;
                // 日志
                UserInfo userInfo = (UserInfo) ServletActionContext.getRequest()
                        .getSession()
                        .getAttribute(Constants.SESSIONPRKEY);
                // 日志记录
                String user = null;
                String cityCode = null;
                String cityName = null;
                String organizaOragName = null;
                if (null == userInfo)
                {
                    user = "张三";
                    cityName = "东莞";
                    organizaOragName = "市场部";
                    cityCode = "DG";
                }
                else
                {
                    user = userInfo.getUserName();
                    cityCode = userInfo.getUsercity();
                    cityName = userInfo.getCityName();
                    organizaOragName = userInfo.getOrganiza().getOrgName();
                }
                if (null == cityCode)
                {
                    cityCode = "";
                    cityName = "东莞";
                }
                Timestamp d = new Timestamp(System.currentTimeMillis());
                String content = d + " " + cityName + organizaOragName + user
                        + "编辑了物料信息";
                String datatime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
                Date data = new Date(System.currentTimeMillis());
                /** 获取变更记录的数据 */
                Log logRecord = new Log("T_CB_PROJECTMEDIUM",
                        content,
                        castDetail.getProMedID() + "",
                        datatime,
                        user,
                        cityCode,
                        organizaOragName,
                        "科室");

                optLogService.addLog(logRecord);
                // }
            }
            else
            {
                // 重置返回值
                resultName = "recordException";
            }
        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction editMaterialInfo() method, return String",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction editMaterialInfo() method, return String");
        }
        return resultName;
    }

    /**
     * 排期信息录入初始化
     * 
     * @return String
     */
    public String initInsertArrangementInfo()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction initInsertArrangementInfo() method, return String");
        }

        try
        {
            // 查询项目媒介
            ProjectMedium pm = castDetailService.getProjectMediumByProMedID(projectMedium.getProMedID());
            if (pm != null)
            {
                Project p = castDetailService.getProjectByProjectID(pm.getProjectID());
                Medium m = castDetailService.getMediumByMediumID(pm.getId_medium());
                String[] mediumArrangement = pm.getMediumArrangement()
                        .split(",");
                String[] totalMediumArrangement = m.getMediumArrangement()
                        .split(",");

                int year = p.getProjectStartTime().getYear() + 1900;
                int startMonth = p.getProjectStartTime().getMonth() + 1;
                int endMonth = p.getProjectEndTime().getMonth() + 1;
                int startDate = p.getProjectStartTime().getDate();
                int endDate = p.getProjectEndTime().getDate();

                String[] weekDay = { "日", "一", "二", "三", "四", "五", "六" };
                String[] weekDayStyle = { "background: #ffffaa;", "", "", "",
                        "", "", "background: #ffffaa;" };
                int[] monthDay = { 31, StringUtils.getFebruary(year), 31, 30,
                        31, 30, 31, 31, 30, 31, 30, 31 };
                int[] totalMonthDay = { 0, 31, 31 + monthDay[1],
                        62 + monthDay[1], 92 + monthDay[1], 123 + monthDay[1],
                        153 + monthDay[1], 184 + monthDay[1],
                        215 + monthDay[1], 245 + monthDay[1],
                        276 + monthDay[1], 306 + monthDay[1] };
                int weekDayIndex = 0;
                //
                int index = totalMonthDay[startMonth - 1] + startDate;
                // 
                initArrangements = new ArrayList<Arrangement>();
                for (int i = startMonth; i <= endMonth; i++)
                {
                    weekDayIndex = new Date(year - 1900, i - 1, 1).getDay();
                    Arrangement arrangement = new Arrangement();
                    arrangement.setYear(year);
                    arrangement.setMonth(i);
                    ArrangementDate[] dates = new ArrangementDate[31];
                    for (int j = 1; j <= 31; j++)
                    {
                        ArrangementDate arrangementDate = new ArrangementDate();
                        arrangementDate.setStyle(weekDayStyle[weekDayIndex]);
                        arrangementDate.setDay(weekDay[weekDayIndex++]);
                        dates[j - 1] = arrangementDate;
                        if (weekDayIndex > 6)
                        {
                            weekDayIndex = 0;
                        }
                        if (i == startMonth)
                        {
                            if (j < startDate)
                            {
                                arrangementDate.setIsShow(1);
                            }
                        }
                        if (i == endMonth)
                        {
                            if (j > endDate)
                            {
                                arrangementDate.setIsShow(1);
                            }
                        }
                        if (j > monthDay[i - 1])
                        {
                            arrangementDate.setIsShow(2);
                        }
                        if (arrangementDate.getIsShow() == 0)
                        {
                            arrangementDate.setInitValue(mediumArrangement[index - 1]);
                            int suggesValue = m.getMediumAbility()
                                    - Integer.valueOf(totalMediumArrangement[index - 1]);
                            arrangementDate.setSuggesValue(suggesValue + "");
                            index++;
                        }
                    }
                    arrangement.setDates(dates);
                    initArrangements.add(arrangement);
                }
                // 重置返回值
                resultName = SUCCESS;
            }
            else
            {
                // 重置返回值
                resultName = "recordException";
            }
        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction initInsertArrangementInfo() method, return String",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction initInsertArrangementInfo() method, return String");
        }
        return resultName;
    }

    /**
     * 排期信息录入
     * 
     * @return String
     */
    public String insertArrangementInfo()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction insertArrangementInfo() method, return String");
        }

        try
        {
            // 查询项目媒介
            ProjectMedium pm = castDetailService.getProjectMediumByProMedID(projectMedium.getProMedID());
            Project p = castDetailService.getProjectByProjectID(pm.getProjectID());
            Medium m = castDetailService.getMediumByMediumID(pm.getId_medium());
            String[] mediumArrangement = pm.getMediumArrangement().split(",");
            String[] totalMediumArrangement = m.getMediumArrangement()
                    .split(",");

            int year = p.getProjectStartTime().getYear() + 1900;
            int startMonth = p.getProjectStartTime().getMonth() + 1;
            int endMonth = p.getProjectEndTime().getMonth() + 1;
            int startDate = p.getProjectStartTime().getDate();
            int endDate = p.getProjectEndTime().getDate();

            int[] monthDay = { 31, StringUtils.getFebruary(year), 31, 30, 31,
                    30, 31, 31, 30, 31, 30, 31 };
            int[] totalMonthDay = { 0, 31, 31 + monthDay[1], 62 + monthDay[1],
                    92 + monthDay[1], 123 + monthDay[1], 153 + monthDay[1],
                    184 + monthDay[1], 215 + monthDay[1], 245 + monthDay[1],
                    276 + monthDay[1], 306 + monthDay[1] };
            // 
            int index = totalMonthDay[startMonth - 1] + startDate;

            String[] newMediumArrangement = new String[mediumArrangement.length];

            StringBuffer sb = new StringBuffer();
            for (int i = 1; i < index; i++)
            {
                sb.append("0,");
            }
            sb.append(projectMedium.getMediumArrangement().substring(0,
                    projectMedium.getMediumArrangement().length() - 1));
            for (int i = (index + projectMedium.getMediumArrangement()
                    .split(",").length); i <= newMediumArrangement.length; i++)
            {
                sb.append(",0");
            }
            pm.setMediumArrangement(sb.toString());
            newMediumArrangement = pm.getMediumArrangement().split(",");
            //
            int totalCount = 0;
            boolean flag = true;
            // 计算
            for (int i = 0; i < mediumArrangement.length; i++)
            {
                int count = Integer.valueOf(totalMediumArrangement[i])
                        - Integer.valueOf(mediumArrangement[i])
                        + Integer.valueOf(newMediumArrangement[i]);
                if (count <= m.getMediumAbility())
                {
                    // 可以修改
                    totalMediumArrangement[i] = count + "";
                    //
                    totalCount += Integer.valueOf(newMediumArrangement[i]);
                }
                else
                {
                    flag = false;
                    break;
                }
            }
            //
            if (flag)
            {
                pm.setMediumAmount(totalCount);
                pm.setMediumTotalPrice(totalCount * m.getMediumPrice());
                pm.setMediumTotalDisprice(Arith.mul(pm.getMediumTotalPrice(),
                        Arith.div(pm.getMediumOrientDiscount(), 100)));

                // 排期是否成功
                boolean bool = castDetailService.updateAllArrangement(totalMediumArrangement,
                        pm,
                        m);
                if (!bool)
                {
                    // 日志
                    UserInfo userInfo = (UserInfo) ServletActionContext.getRequest()
                            .getSession()
                            .getAttribute(Constants.SESSIONPRKEY);
                    // 日志记录
                    String user = null;
                    String cityCode = null;
                    String cityName = null;
                    String organizaOragName = null;
                    if (null == userInfo)
                    {
                        user = "张三";
                        cityName = "东莞";
                        organizaOragName = "市场部";
                        cityCode = "DG";
                    }
                    else
                    {
                        user = userInfo.getUserName();
                        cityCode = userInfo.getUsercity();
                        cityName = userInfo.getCityName();
                        organizaOragName = userInfo.getOrganiza().getOrgName();
                    }
                    if (null == cityCode)
                    {
                        cityCode = "";
                        cityName = "东莞";
                    }
                    Timestamp d = new Timestamp(System.currentTimeMillis());
                    String content = d + " " + cityName + organizaOragName
                            + user + "创建了排期信息";
                    String datatime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
                    Date data = new Date(System.currentTimeMillis());
                    /** 获取变更记录的数据 */
                    Log logRecord = new Log("T_CB_PROJECTMEDIUM",
                            content,
                            projectMedium.getProMedID() + "",
                            datatime,
                            user,
                            cityCode,
                            organizaOragName,
                            "科室");

                    optLogService.addLog(logRecord);
                    // 重置返回值
                    resultName = SUCCESS;
                }
                else
                {
                    HttpServletResponse response = ServletActionContext.getResponse();
                    response.setContentType("text/html; charset=utf-8");
                    PrintWriter out = response.getWriter();
                    out.println("<script type=\"text/javascript\">");
                    out.println("alert(\"排期被修改，排期不成功！\");");
                    out.println("window.location.href='mediumCastDetailList.action';");
                    out.println("</script>");
                    out.flush();
                    out.close();
                }

            }
            else
            {

                HttpServletResponse response = ServletActionContext.getResponse();
                response.setContentType("text/html; charset=utf-8");
                PrintWriter out = response.getWriter();
                out.println("<script type=\"text/javascript\">");
                out.println("alert(\"排期冲突，媒介能力超过上限，排期不成功！\");");
                out.println("window.location.href='mediumCastDetailList.action';");
                out.println("</script>");
                out.flush();
                out.close();
            }
        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction insertArrangementInfo() method, return String",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction insertArrangementInfo() method, return String");
        }
        return resultName;
    }

    /**
     * 排期信息编辑初始化
     * 
     * @return String
     */
    public String initEditArrangementInfo()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction initEditArrangementInfo() method, return String");
        }

        try
        {
            // 查询项目媒介
            ProjectMedium pm = castDetailService.getProjectMediumByProMedID(projectMedium.getProMedID());
            if (pm != null)
            {
                if (isInsertFlag == 1)
                {
                    Project p = castDetailService.getProjectByProjectID(pm.getProjectID());
                    Medium m = castDetailService.getMediumByMediumID(pm.getId_medium());
                    String[] mediumArrangement = pm.getMediumArrangement()
                            .split(",");
                    String[] totalMediumArrangement = m.getMediumArrangement()
                            .split(",");

                    int year = p.getProjectStartTime().getYear() + 1900;
                    int startMonth = p.getProjectStartTime().getMonth() + 1;
                    int endMonth = p.getProjectEndTime().getMonth() + 1;
                    int startDate = p.getProjectStartTime().getDate();
                    int endDate = p.getProjectEndTime().getDate();

                    String[] weekDay = { "日", "一", "二", "三", "四", "五", "六" };
                    String[] weekDayStyle = { "background: #ffffaa;", "",
                            "", "", "", "", "background: #ffffaa;" };
                    int[] monthDay = { 31, StringUtils.getFebruary(year),
                            31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
                    int[] totalMonthDay = { 0, 31, 31 + monthDay[1],
                            62 + monthDay[1], 92 + monthDay[1],
                            123 + monthDay[1], 153 + monthDay[1],
                            184 + monthDay[1], 215 + monthDay[1],
                            245 + monthDay[1], 276 + monthDay[1],
                            306 + monthDay[1] };
                    int weekDayIndex = 0;
                    //
                    int index = totalMonthDay[startMonth - 1] + startDate;
                    // 
                    initArrangements = new ArrayList<Arrangement>();
                    for (int i = startMonth; i <= endMonth; i++)
                    {
                        weekDayIndex = new Date(year - 1900, i - 1, 1).getDay();
                        Arrangement arrangement = new Arrangement();
                        arrangement.setYear(year);
                        arrangement.setMonth(i);
                        ArrangementDate[] dates = new ArrangementDate[31];
                        for (int j = 1; j <= 31; j++)
                        {
                            ArrangementDate arrangementDate = new ArrangementDate();
                            arrangementDate.setStyle(weekDayStyle[weekDayIndex]);
                            arrangementDate.setDay(weekDay[weekDayIndex++]);
                            dates[j - 1] = arrangementDate;
                            if (weekDayIndex > 6)
                            {
                                weekDayIndex = 0;
                            }
                            if (i == startMonth)
                            {
                                if (j < startDate)
                                {
                                    arrangementDate.setIsShow(1);
                                }
                            }
                            if (i == endMonth)
                            {
                                if (j > endDate)
                                {
                                    arrangementDate.setIsShow(1);
                                }
                            }
                            if (j > monthDay[i - 1])
                            {
                                arrangementDate.setIsShow(2);
                            }
                            if (arrangementDate.getIsShow() == 0)
                            {
                                arrangementDate.setInitValue(mediumArrangement[index - 1]);
                                int suggesValue = m.getMediumAbility()
                                        - Integer.valueOf(totalMediumArrangement[index - 1])
                                        + Integer.valueOf(arrangementDate.getInitValue());
                                arrangementDate.setSuggesValue(suggesValue
                                        + "");
                                index++;
                            }
                        }
                        arrangement.setDates(dates);
                        initArrangements.add(arrangement);
                    }
                    // 重置返回值
                    resultName = SUCCESS;
                }
                else
                {
                    if (Constants.MJSYQK_4.equals(pm.getMediumStatus())
                            || Constants.MJSYQK_5.equals(pm.getMediumStatus())
                            || Constants.MJSYQK_6.equals(pm.getMediumStatus()))
                    {
                        HttpServletResponse response = ServletActionContext.getResponse();
                        response.setContentType("text/html; charset=utf-8");
                        PrintWriter out = response.getWriter();
                        out.println("<script type=\"text/javascript\">");
                        // out.println("alert(\"该条记录已完成投放或已完成结算或已中止不可编辑！\");");
                        out.println("window.location.href='mediumCastDetailList.action';");
                        out.println("</script>");
                        out.flush();
                        out.close();
                    }
                    else
                    {

                        Project p = castDetailService.getProjectByProjectID(pm.getProjectID());
                        Medium m = castDetailService.getMediumByMediumID(pm.getId_medium());
                        String[] mediumArrangement = pm.getMediumArrangement()
                                .split(",");
                        String[] totalMediumArrangement = m.getMediumArrangement()
                                .split(",");

                        int year = p.getProjectStartTime().getYear() + 1900;
                        int startMonth = p.getProjectStartTime().getMonth() + 1;
                        int endMonth = p.getProjectEndTime().getMonth() + 1;
                        int startDate = p.getProjectStartTime().getDate();
                        int endDate = p.getProjectEndTime().getDate();

                        String[] weekDay = { "日", "一", "二", "三", "四", "五", "六" };
                        String[] weekDayStyle = { "background: #ffffaa;", "",
                                "", "", "", "", "background: #ffffaa;" };
                        int[] monthDay = { 31, StringUtils.getFebruary(year),
                                31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
                        int[] totalMonthDay = { 0, 31, 31 + monthDay[1],
                                62 + monthDay[1], 92 + monthDay[1],
                                123 + monthDay[1], 153 + monthDay[1],
                                184 + monthDay[1], 215 + monthDay[1],
                                245 + monthDay[1], 276 + monthDay[1],
                                306 + monthDay[1] };
                        int weekDayIndex = 0;
                        //
                        int index = totalMonthDay[startMonth - 1] + startDate;
                        // 
                        initArrangements = new ArrayList<Arrangement>();
                        for (int i = startMonth; i <= endMonth; i++)
                        {
                            weekDayIndex = new Date(year - 1900, i - 1, 1).getDay();
                            Arrangement arrangement = new Arrangement();
                            arrangement.setYear(year);
                            arrangement.setMonth(i);
                            ArrangementDate[] dates = new ArrangementDate[31];
                            for (int j = 1; j <= 31; j++)
                            {
                                ArrangementDate arrangementDate = new ArrangementDate();
                                arrangementDate.setStyle(weekDayStyle[weekDayIndex]);
                                arrangementDate.setDay(weekDay[weekDayIndex++]);
                                dates[j - 1] = arrangementDate;
                                if (weekDayIndex > 6)
                                {
                                    weekDayIndex = 0;
                                }
                                if (i == startMonth)
                                {
                                    if (j < startDate)
                                    {
                                        arrangementDate.setIsShow(1);
                                    }
                                }
                                if (i == endMonth)
                                {
                                    if (j > endDate)
                                    {
                                        arrangementDate.setIsShow(1);
                                    }
                                }
                                if (j > monthDay[i - 1])
                                {
                                    arrangementDate.setIsShow(2);
                                }
                                if (arrangementDate.getIsShow() == 0)
                                {
                                    arrangementDate.setInitValue(mediumArrangement[index - 1]);
                                    int suggesValue = m.getMediumAbility()
                                            - Integer.valueOf(totalMediumArrangement[index - 1])
                                            + Integer.valueOf(arrangementDate.getInitValue());
                                    arrangementDate.setSuggesValue(suggesValue
                                            + "");
                                    index++;
                                }
                            }
                            arrangement.setDates(dates);
                            initArrangements.add(arrangement);
                        }
                        // 重置返回值
                        resultName = SUCCESS;
                    }
                }
            }
            else
            {
                // 重置返回值
                resultName = "recordException";
            }
        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction initEditArrangementInfo() method, return String",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction initInsertArrangementInfo() method, return String");
        }
        return resultName;
    }

    /**
     * 排期信息录入
     * 
     * @return String
     */
    public String editArrangementInfo()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction insertArrangementInfo() method, return String");
        }

        try
        {
            // 查询项目媒介
            ProjectMedium pm = castDetailService.getProjectMediumByProMedID(projectMedium.getProMedID());
            if (pm != null)
            {
                //
                // if (Constants.MJSYQK_4.equals(pm.getMediumStatus())
                // || Constants.MJSYQK_5.equals(pm.getMediumStatus())
                // || Constants.MJSYQK_6.equals(pm.getMediumStatus()))
                // {
                // HttpServletResponse response =
                // ServletActionContext.getResponse();
                // response.setContentType("text/html; charset=utf-8");
                // PrintWriter out = response.getWriter();
                // out.println("<script type=\"text/javascript\">");
                // out.println("alert(\"该条记录已完成投放或已完成结算或已中止不可编辑！\");");
                // out.println("window.location.href='mediumCastDetailList.action';");
                // out.println("</script>");
                // out.flush();
                // out.close();
                // }
                // else
                // {

                Project p = castDetailService.getProjectByProjectID(pm.getProjectID());
                Medium m = castDetailService.getMediumByMediumID(pm.getId_medium());
                String[] mediumArrangement = pm.getMediumArrangement()
                        .split(",");
                String[] totalMediumArrangement = m.getMediumArrangement()
                        .split(",");

                int year = p.getProjectStartTime().getYear() + 1900;
                int startMonth = p.getProjectStartTime().getMonth() + 1;
                int endMonth = p.getProjectEndTime().getMonth() + 1;
                int startDate = p.getProjectStartTime().getDate();
                int endDate = p.getProjectEndTime().getDate();

                int[] monthDay = { 31, StringUtils.getFebruary(year), 31, 30,
                        31, 30, 31, 31, 30, 31, 30, 31 };
                int[] totalMonthDay = { 0, 31, 31 + monthDay[1],
                        62 + monthDay[1], 92 + monthDay[1], 123 + monthDay[1],
                        153 + monthDay[1], 184 + monthDay[1],
                        215 + monthDay[1], 245 + monthDay[1],
                        276 + monthDay[1], 306 + monthDay[1] };
                // 
                int index = totalMonthDay[startMonth - 1] + startDate;

                String[] newMediumArrangement = new String[mediumArrangement.length];

                StringBuffer sb = new StringBuffer();
                for (int i = 1; i < index; i++)
                {
                    sb.append("0,");
                }
                sb.append(projectMedium.getMediumArrangement().substring(0,
                        projectMedium.getMediumArrangement().length() - 1));
                for (int i = (index + projectMedium.getMediumArrangement()
                        .split(",").length); i <= newMediumArrangement.length; i++)
                {
                    sb.append(",0");
                }
                pm.setMediumArrangement(sb.toString());
                newMediumArrangement = pm.getMediumArrangement().split(",");
                //
                int totalCount = 0;
                boolean flag = true;
                // 计算
                for (int i = 0; i < mediumArrangement.length; i++)
                {
                    int count = Integer.valueOf(totalMediumArrangement[i])
                            - Integer.valueOf(mediumArrangement[i])
                            + Integer.valueOf(newMediumArrangement[i]);
                    if (count <= m.getMediumAbility())
                    {
                        // 可以修改
                        totalMediumArrangement[i] = count + "";
                        //
                        totalCount += Integer.valueOf(newMediumArrangement[i]);
                    }
                    else
                    {
                        flag = false;
                        break;
                    }
                }
                //
                if (flag)
                {
                    pm.setMediumAmount(totalCount);
                    pm.setMediumTotalPrice(totalCount * m.getMediumPrice());
                    pm.setMediumTotalDisprice(pm.getMediumTotalPrice()
                            * pm.getMediumOrientDiscount() / 100);
                    boolean bool = castDetailService.updateAllArrangement(totalMediumArrangement,
                            pm,
                            m);
                    if (!bool)
                    {
                        // 重置返回值
                        resultName = SUCCESS;
                        // 日志
                        UserInfo userInfo = (UserInfo) ServletActionContext.getRequest()
                                .getSession()
                                .getAttribute(Constants.SESSIONPRKEY);
                        // 日志记录
                        String user = null;
                        String cityCode = null;
                        String cityName = null;
                        String organizaOragName = null;
                        if (null == userInfo)
                        {
                            user = "张三";
                            cityName = "东莞";
                            organizaOragName = "市场部";
                            cityCode = "DG";
                        }
                        else
                        {
                            user = userInfo.getUserName();
                            cityCode = userInfo.getUsercity();
                            cityName = userInfo.getCityName();
                            organizaOragName = userInfo.getOrganiza()
                                    .getOrgName();
                        }
                        if (null == cityCode)
                        {
                            cityCode = "";
                            cityName = "东莞";
                        }
                        Timestamp d = new Timestamp(System.currentTimeMillis());
                        String content = d + " " + cityName + organizaOragName
                                + user + "编辑了排期信息";
                        String datatime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
                        Date data = new Date(System.currentTimeMillis());
                        /** 获取变更记录的数据 */
                        Log logRecord = new Log("T_CB_PROJECTMEDIUM",
                                content,
                                projectMedium.getProMedID() + "",
                                datatime,
                                user,
                                cityCode,
                                organizaOragName,
                                "科室");

                        optLogService.addLog(logRecord);
                    }
                    else
                    {
                        HttpServletResponse response = ServletActionContext.getResponse();
                        response.setContentType("text/html; charset=utf-8");
                        PrintWriter out = response.getWriter();
                        out.println("<script type=\"text/javascript\">");
                        out.println("alert(\"排期被修改，排期不成功！\");");
                        out.println("window.location.href='initCastDetailHome.action?projectMedium.proMedID="
                                + projectMedium.getProMedID() + "';");
                        out.println("</script>");
                        out.flush();
                        out.close();
                    }
                }
                else
                {

                    HttpServletResponse response = ServletActionContext.getResponse();
                    response.setContentType("text/html; charset=utf-8");
                    PrintWriter out = response.getWriter();
                    out.println("<script type=\"text/javascript\">");
                    out.println("alert(\"排期冲突，媒介能力超过上限，排期不成功！\");");
                    out.println("window.location.href='initCastDetailHome.action?projectMedium.proMedID="
                            + projectMedium.getProMedID() + "';");
                    out.println("</script>");
                    out.flush();
                    out.close();
                }
                // }
            }
            else
            {
                // 重置返回值
                resultName = "recordException";
            }
        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction insertArrangementInfo() method, return String",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction insertArrangementInfo() method, return String");
        }
        return resultName;
    }

    /**
     * ajax获取所有项目
     * 
     * @return String
     */
    public String ajaxGetProject()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction ajaxGetProject() method, return String");
        }

        try
        {
            // 实例
            Project project = new Project();
            //
            if(keyword != null){
                keyword = keyword.trim();
            }
            // 关键字
            project.setProjectName(keyword);
            // 获取当前session对象中所属城市
            UserInfo userInfo = (UserInfo) ServletActionContext.getRequest()
                    .getSession()
                    .getAttribute(Constants.SESSIONPRKEY);
            // 非空
            if (null != userInfo)
            {
                //
                if (userInfo.getUserRole() != null)
                {
                    // 操作范围
                    String roleRange = userInfo.getUserRole().getRoleRange();
                    //
                    if (roleRange != null)
                    {
                        // 默认东莞
                        String userCity = "DG";
                        // 非空重置
                        if (userInfo.getUsercity() != null)
                        {
                            if (!"".equals(userInfo.getUsercity()))
                            {
                                userCity = userInfo.getUsercity();
                            }
                        }
                        // 地市
                        project.setCity(userCity);

                        // 个人
                        if ("3".endsWith(roleRange))
                        {
                            // 
                            project.setProjectManager(userInfo.getUserCode());
                            // 查询
                            ajaxProjects = castDetailService.ajaxGetProject(project);
                        }
                        else
                        {
                            // 查询
                            ajaxProjects = castDetailService.ajaxGetProject(project);
                        }
                        // // 省
                        // if ("1".endsWith(roleRange))
                        // {
                        // // 查询
                        // ajaxProjects =
                        // castDetailService.ajaxGetProject(project);
                        // }
                        // // 地市级
                        // else if ("2".endsWith(roleRange))
                        // {
                        //                            
                        // }

                    }
                }
            }
            // 重置返回值
            resultName = SUCCESS;
        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction ajaxGetProject() method",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction ajaxGetProject() method, return String");
        }
        return resultName;
    }

    /**
     * ajax获取所有媒体
     * 
     * @return String
     */
    public String ajaxGetMedia()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction ajaxGetMedia() method, return String");
        }

        try
        {
            // 获取当前session对象中所属城市
            UserInfo userInfo = (UserInfo) ServletActionContext.getRequest()
                    .getSession()
                    .getAttribute(Constants.SESSIONPRKEY);
            // 非空
            if (null != userInfo)
            {
                // 实例化
                MediaPrice mediaPrice = new MediaPrice();
                // 年度
                mediaPrice.setContractYear((new Date().getYear() + 1900) + "");
                if(keyword != null){
                    keyword = keyword.trim();
                }
                // 媒体名称模糊查询
                mediaPrice.setMediaBelog(keyword);
                if (userInfo.getUserRole() != null)
                {
                    // 操作范围
                    String roleRange = userInfo.getUserRole().getRoleRange();
                    //
                    if (roleRange != null)
                    {

                        // 默认东莞
                        String userCity = "DG";
                        // 非空重置
                        if (userInfo.getUsercity() != null)
                        {
                            if (!"".equals(userInfo.getUsercity()))
                            {
                                userCity = userInfo.getUsercity();
                            }
                        }

                        // 地市
                        mediaPrice.setCity(userCity);

                        // 查询媒体
                        ajaxMedias = castDetailService.ajaxGetMedia(mediaPrice);

                        // // 省
                        // if ("1".endsWith(roleRange))
                        // {
                        //                            
                        // }
                        // // 地市级&个人
                        // else if ("2".endsWith(roleRange)
                        // || "3".endsWith(roleRange))
                        // {
                        // // 查询媒体
                        // ajaxMedias =
                        // castDetailService.ajaxGetMedia(mediaPrice);
                        // }
                    }
                }
            }
            // 实例化
            if (ajaxMedias == null)
            {
                ajaxMedias = new ArrayList<Mediasumm>();
            }
            // 重置返回值
            resultName = SUCCESS;
        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction ajaxGetMedia() method", e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction ajaxGetMedia() method, return String");
        }
        return resultName;
    }

    /**
     * ajax获取某个媒体下的所有媒介
     * 
     * @return String
     */
    public String ajaxGetMedium()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction ajaxGetMedium() method, return String");
        }

        try
        {
            //
            Medium medium = new Medium();
            medium.setMediaID(Integer.parseInt(projectMedium.getMediaID()));
            if(keyword != null){
                keyword = keyword.trim();
            }
            medium.setMediumName(keyword);
            //
            ajaxMediums = castDetailService.ajaxGetMedium(medium);
            // 重置返回值
            resultName = SUCCESS;
        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction ajaxGetMedium() method",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction ajaxGetMedium() method, return String");
        }
        return resultName;
    }

    /**
     * ajax获取所有物料
     * 
     * @return String
     */
    public String ajaxGetMaterial()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction ajaxGetMaterial() method, return String");
        }

        try
        {
            // 获取当前session对象中所属城市
            UserInfo userInfo = (UserInfo) ServletActionContext.getRequest()
                    .getSession()
                    .getAttribute(Constants.SESSIONPRKEY);
            // 非空
            if (null != userInfo)
            {
                // 实例化
                Material material = new Material();
                if(keyword != null){
                    keyword = keyword.trim();
                }
                material.setMaterialName(keyword);
                material.setMaterialLevel(g_materialLevel);
                //
                if (userInfo.getUserRole() != null)
                {
                    // 操作范围
                    String roleRange = userInfo.getUserRole().getRoleRange();
                    //
                    if (roleRange != null)
                    {
                        // 省
                        if ("1".endsWith(roleRange))
                        {
                            // 查询媒体
                            ajaxMaterials = castDetailService.ajaxGetMaterial(material);
                        }
                        // 地市级&个人
                        else if ("2".endsWith(roleRange)
                                || "3".endsWith(roleRange))
                        {
                            // 默认东莞
                            String userCity = "DG";
                            // 非空重置
                            if (userInfo.getUsercity() != null)
                            {
                                if (!"".equals(userInfo.getUsercity()))
                                {
                                    userCity = userInfo.getUsercity();
                                }
                            }
                            // 地市
                            material.setOwnCity(userCity);
                            // 查询媒体
                            ajaxMaterials = castDetailService.ajaxGetMaterial(material);
                        }
                    }
                }
            }
            // 重置返回值
            resultName = SUCCESS;
        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction ajaxGetMaterial() method",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction ajaxGetMaterial() method, return String");
        }
        return resultName;
    }

    /**
     * ajax获取所有物料
     * 
     * @return String
     */
    public String ajaxCastDetail()
    {
        // 返回值
        String resultName = null;
        // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter CastDetailAction ajaxCastDetail() method, return String");
        }

        Log sample = new Log();
        sample.setRelationId(proMedID + "");
        sample.setLogType("T_CB_PROJECTMEDIUM");
        logList = new ArrayList<Log>();
        logList = optLogService.getLogsBySample(sample);

        try
        {
            //
            projectMedium = new ProjectMedium();
            projectMedium.setProMedID(proMedID);
            //
            castDetail = castDetailService.getMediumCastDetail(projectMedium);
            //
            if (castDetail != null)
            {
                //
                castDetail.setMediumStatus(DatadicContext.getInstance()
                        .getDatadicObject(castDetail.getMediumStatus(),
                                "MJSYQK")
                        .getDatavalue());
                if (castDetail.getId_material() != null
                        && !"".equals(castDetail.getId_material()))
                {
                    Material material1 = new Material();
                    material1.setMaterialID(castDetail.getId_material());
                    ajaxMaterials = castDetailService.ajaxGetMaterial(material1);
                    if (ajaxMaterials != null)
                    {
                        if (ajaxMaterials.size() != 0)
                        {
                            String[] id_materialArray = new String[ajaxMaterials.size()];
                            for (int i = 0; i < ajaxMaterials.size(); i++)
                            {
                                id_materialArray[i] = ajaxMaterials.get(i)
                                        .getMaterialID();
                            }
                            castDetail.setId_materialArray(id_materialArray);
                            //
                            String[][] array = new String[ajaxMaterials.size()][2];
                            for (int i = 0; i < ajaxMaterials.size(); i++)
                            {
                                array[i][0] = ajaxMaterials.get(i).getMatId()
                                        + "";
                                array[i][1] = ajaxMaterials.get(i)
                                        .getMaterialID();
                            }
                            castDetail.setMaterialsArray(array);
                        }
                    }
                }
            }
            //
            // 查询项目媒介
            pm = castDetailService.getProjectMediumByProMedID(projectMedium.getProMedID());
            Project p = castDetailService.getProjectByProjectID(pm.getProjectID());
            Medium m = castDetailService.getMediumByMediumID(pm.getId_medium());
            pm.setMediumID(m.getMediumPrice() + "");
            String[] mediumArrangement = pm.getMediumArrangement().split(",");
            String[] totalMediumArrangement = m.getMediumArrangement()
                    .split(",");

            int year = p.getProjectStartTime().getYear() + 1900;
            int startMonth = p.getProjectStartTime().getMonth() + 1;
            int endMonth = p.getProjectEndTime().getMonth() + 1;
            int startDate = p.getProjectStartTime().getDate();
            int endDate = p.getProjectEndTime().getDate();

            String[] weekDay = { "日", "一", "二", "三", "四", "五", "六" };
            String[] weekDayStyle = { "background: #ffffaa;", "", "", "", "",
                    "", "background: #ffffaa;" };
            int[] monthDay = { 31, StringUtils.getFebruary(year), 31, 30, 31,
                    30, 31, 31, 30, 31, 30, 31 };
            int[] totalMonthDay = { 0, 31, 31 + monthDay[1], 62 + monthDay[1],
                    92 + monthDay[1], 123 + monthDay[1], 153 + monthDay[1],
                    184 + monthDay[1], 215 + monthDay[1], 245 + monthDay[1],
                    276 + monthDay[1], 306 + monthDay[1] };
            int weekDayIndex = 0;
            //
            int index = totalMonthDay[startMonth - 1] + startDate;
            // 
            initArrangements = new ArrayList<Arrangement>();
            for (int i = startMonth; i <= endMonth; i++)
            {
                weekDayIndex = new Date(year - 1900, i - 1, 1).getDay();
                Arrangement arrangement = new Arrangement();
                arrangement.setYear(year);
                arrangement.setMonth(i);
                ArrangementDate[] dates = new ArrangementDate[31];
                for (int j = 1; j <= 31; j++)
                {
                    ArrangementDate arrangementDate = new ArrangementDate();
                    arrangementDate.setStyle(weekDayStyle[weekDayIndex]);
                    arrangementDate.setDay(weekDay[weekDayIndex++]);
                    dates[j - 1] = arrangementDate;
                    if (weekDayIndex > 6)
                    {
                        weekDayIndex = 0;
                    }
                    if (i == startMonth)
                    {
                        if (j < startDate)
                        {
                            arrangementDate.setIsShow(1);
                        }
                    }
                    if (i == endMonth)
                    {
                        if (j > endDate)
                        {
                            arrangementDate.setIsShow(1);
                        }
                    }
                    if (j > monthDay[i - 1])
                    {
                        arrangementDate.setIsShow(2);
                    }
                    if (arrangementDate.getIsShow() == 0)
                    {
                        arrangementDate.setInitValue(mediumArrangement[index - 1]);
                        index++;
                    }
                }
                arrangement.setDates(dates);
                initArrangements.add(arrangement);
            }
            // 费用
            charges = chargeService.getChargesByProMedID(proMedID);
            for (Charge charge : charges)
            {
                if (StringUtils.isEmpty(charge.getCostType()))
                {
                    charge.setCostType("无");
                }
                else
                {
                    charge.setCostType(DatadicContext.getInstance()
                            .getDatadicObject(charge.getCostType(), "FYLX")
                            .getDatavalue());
                }

                if (StringUtils.isEmpty(charge.getCostSubtype()))
                {
                    charge.setCostSubtype("无");
                }
                else
                {
                    charge.setCostSubtype(DatadicContext.getInstance()
                            .getDatadicObject(charge.getCostSubtype(), "FYLX")
                            .getDatavalue());
                }

                charge.setFundType(DatadicContext.getInstance()
                        .getDatadicObject(charge.getFundType(), "ZJLX")
                        .getDatavalue());
                charge.setJljfType(DatadicContext.getInstance()
                        .getDatadicObject(charge.getJljfType(), "JLJFLX")
                        .getDatavalue());
                if (charge.getTsbjType() != null)
                {
                    if (!"".equals(charge.getTsbjType()))
                    {
                        charge.setTsbjType(DatadicContext.getInstance()
                                .getDatadicObject(charge.getTsbjType(),
                                        "TSBJLX")
                                .getDatavalue());
                    }
                }

            }
            // 重置返回值
            resultName = SUCCESS;
            //
            ajaxFlag = true;
        }
        catch (Exception e)
        {
            // 打印异常信息
            e.printStackTrace();
            // 打印异常方法体日志
            if (log.isInfoEnabled())
            {
                log.error("Exception CastDetailAction ajaxCastDetail() method",
                        e);
            }
            // 重置返回值
            resultName = ERROR;
        }

        // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit CastDetailAction ajaxCastDetail() method, return String");
        }
        return resultName;
    }

    public String initCastDetailHome()
    {
        if (projectMedium == null)
        {
            projectMedium = new ProjectMedium();
        }

        return SUCCESS;
    }

    public void setCastDetailService(CastDetailService castDetailService)
    {
        this.castDetailService = castDetailService;
    }

    public void setChargeService(ChargeService chargeService)
    {
        this.chargeService = chargeService;
    }

    public int getCurrentPage()
    {
        return currentPage;
    }

    public void setCurrentPage(int currentPage)
    {
        this.currentPage = currentPage;
    }

    public PageBean getPage()
    {
        return page;
    }

    public void setPage(PageBean page)
    {
        this.page = page;
    }

    public List<CastDetail> getCastDetails()
    {
        return castDetails;
    }

    public String getProjectStartTime()
    {
        return projectStartTime;
    }

    public void setProjectStartTime(String projectStartTime)
    {
        this.projectStartTime = projectStartTime;
    }

    public String getProjectEndTime()
    {
        return projectEndTime;
    }

    public void setProjectEndTime(String projectEndTime)
    {
        this.projectEndTime = projectEndTime;
    }

    public String getMediumResourceType()
    {
        return mediumResourceType;
    }

    public void setMediumResourceType(String mediumResourceType)
    {
        this.mediumResourceType = mediumResourceType;
    }

    public String getMediumStatus()
    {
        return mediumStatus;
    }

    public void setMediumStatus(String mediumStatus)
    {
        this.mediumStatus = mediumStatus;
    }

    public String getCity()
    {
        return city;
    }

    public void setCity(String city)
    {
        this.city = city;
    }

    public String getKeywordType()
    {
        return keywordType;
    }

    public void setKeywordType(String keywordType)
    {
        this.keywordType = keywordType;
    }

    public String getKeyword()
    {
        return keyword;
    }

    public void setKeyword(String keyword)
    {
        this.keyword = keyword;
    }

    public String getProjectType()
    {
        return projectType;
    }

    public void setProjectType(String projectType)
    {
        this.projectType = projectType;
    }

    public void setProMedID(int proMedID)
    {
        this.proMedID = proMedID;
    }

    public List<Dictionary> getMediaResourceTypeSelect()
    {
        return mediaResourceTypeSelect;
    }

    public List<Dictionary> getMediumStatusSelect()
    {
        return mediumStatusSelect;
    }

    public List<Dictionary> getCitySelect()
    {
        return citySelect;
    }

    public List<Dictionary> getKeywordTypeSelect()
    {
        return keywordTypeSelect;
    }

    public List<Dictionary> getProjectTypeSelect()
    {
        return projectTypeSelect;
    }

    public List<Project> getAjaxProjects()
    {
        return ajaxProjects;
    }

    public void setAjaxProjects(List<Project> ajaxProjects)
    {
        this.ajaxProjects = ajaxProjects;
    }

    public List<Mediasumm> getAjaxMedias()
    {
        return ajaxMedias;
    }

    public void setAjaxMedias(List<Mediasumm> ajaxMedias)
    {
        this.ajaxMedias = ajaxMedias;
    }

    public List<Medium> getAjaxMediums()
    {
        return ajaxMediums;
    }

    public void setAjaxMediums(List<Medium> ajaxMediums)
    {
        this.ajaxMediums = ajaxMediums;
    }

    public List<Material> getAjaxMaterials()
    {
        return ajaxMaterials;
    }

    public void setAjaxMaterials(List<Material> ajaxMaterials)
    {
        this.ajaxMaterials = ajaxMaterials;
    }

    public ProjectMedium getProjectMedium()
    {
        return projectMedium;
    }

    public void setProjectMedium(ProjectMedium projectMedium)
    {
        this.projectMedium = projectMedium;
    }

    public List<Arrangement> getInitArrangements()
    {
        return initArrangements;
    }

    public CastDetail getCastDetail()
    {
        return castDetail;
    }

    public void setCastDetail(CastDetail castDetail)
    {
        this.castDetail = castDetail;
    }

    public List<Dictionary> getMediumPromotionTypeSelect()
    {
        return mediumPromotionTypeSelect;
    }

    public List<Charge> getCharges()
    {
        return charges;
    }

    public void setOptLogService(OptLogService optLogService)
    {
        this.optLogService = optLogService;
    }

    public List<Log> getLogList()
    {
        return logList;
    }

    public String getUserRoleRange()
    {
        return userRoleRange;
    }

    public boolean isAjaxFlag()
    {
        return ajaxFlag;
    }

    public void setCastDetailstatus(int castDetailstatus)
    {
        this.castDetailstatus = castDetailstatus;
    }

    public int getCastDetailstatus()
    {
        return castDetailstatus;
    }

    ProjectMedium pm;

    public ProjectMedium getPm()
    {
        return pm;
    }

    public Material getG_material()
    {
        return g_material;
    }

    public void setG_material(Material gMaterial)
    {
        g_material = gMaterial;
    }

    public String getsProjectId()
    {
        return sProjectId;
    }

    public void setsProjectId(String sProjectId)
    {
        this.sProjectId = sProjectId;
    }

    public String getG_materialLevel()
    {
        return g_materialLevel;
    }

    public void setG_materialLevel(String gMaterialLevel)
    {
        g_materialLevel = gMaterialLevel;
    }

    public int getIsInsertFlag()
    {
        return isInsertFlag;
    }

    public void setIsInsertFlag(int isInsertFlag)
    {
        this.isInsertFlag = isInsertFlag;
    }

}
